package com.dao.Impl;

import com.dao.CardDao;
import com.entity.Card;
import com.entity.PageBean;
import com.util.JDBC;
import com.util.TransBean;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author 沐白
 */
public class CardDaoImpl implements CardDao {
    @Override
    public int add(Card card) {
        return JDBC.sqlUpdate("insert into card(id,cardId,userId,amount,credit,status,staffId,levelId) values(null,?,?,?,?,?,?,?)", card.getCardId(), card.getUserId(), card.getAmount(), card.getCredit(), card.getStatus(), card.getStaffId(), card.getLevelId());
    }

    @Override
    public List<Card> findAll() {
        List<Map> maps = JDBC.selectMap("select id,cardId,userId,amount,credit,status,staffId,levelId from card");
        List<Card> clist = TransBean.populate(Card.class, maps);
        return clist;
    }

    @Override
    public void findAllPage(PageBean pageBean) {
        String sql = "select count(*) from card where 1=1 %s";
        String whereStr = "";
        String[] factor = pageBean.getFactor();
        if(factor!=null){
            for (String s : factor) {
                whereStr+=" and "+s;
            }
        }
        sql=String.format(sql, whereStr);
        List<List> list = JDBC.selectArray(sql);
        int count = ((Long) list.get(0).get(0)).intValue();
        pageBean.setRowCount(count);


        sql = "select id,cardId,userId,amount,credit,status,staffId,levelId from card " +
                " where 1=1 %s limit ?,?";
        sql=String.format(sql, whereStr);
        List<Map> maps = JDBC.selectMap(sql,pageBean.getStartIndex(),pageBean.getPageSize());
        pageBean.setData(TransBean.populate(Card.class, maps));
    }

    @Override
    public Card findById(int cardId) {
        List<Map> maps = JDBC.selectMap("select id,cardId,userId,amount,credit,status,staffId,levelId from card where cardId=?", cardId);
        List<Card> clist = TransBean.populate(Card.class, maps);
        if(clist!=null){
            return clist.get(0);
        }
        return null;
    }

    @Override
    public int modifyStatus(Card card) {
        return JDBC.sqlUpdate("update card set status=? where userId=?",card.getStatus(),card.getUserId());
    }

    @Override
    public int delete(int id) {
        return 0;
    }

    @Override
    public List<Card> findSctn(int page, int count) {
        return null;
    }

    @Override
    public List<Card> findAmout() {
        return null;
    }


    /**
     * 查询Card表中cardId的最后一个
     * @return
     */
    @Override
    public int cardIdEnd() {
        List<List> lists = JDBC.selectArray("select cardId from card order by id desc limit 1");
        if (lists.size()>0){
            int cardId=(Integer)(lists.get(0).get(0));
            return cardId;
        }
        return -1;
    }


    /**
     * 会员充值
     * @param card
     */
    @Override
    public void updateAmount(Card card) {
        JDBC.sqlUpdate("update card set amount=? where cardId=?",card.getAmount(),card.getCardId());


    }

    /**
     * 会员状态修改
     * @param card
     */
    @Override
    public void updateStatus(Card card) {
        JDBC.sqlUpdate("update card set status=? where cardId=?",card.getStatus(),card.getCardId());
    }

    @Override
    public int updateAmount(int cardId, double amount,int credit) {
        return JDBC.sqlUpdate("update card set amount=?,credit=credit+? where cardId=?",
                amount,credit,cardId);
    }


    /**
     * 会员基本资料
     * @param cardId
     * @return
     */
    @Override
    public List<Map> findCardBasic(int cardId) {
        List<Map> list = JDBC.selectMap("select u.userId,userName,phone,address,idCard,c.status,c.cardId,amount,sex,ca.name,c.credit from user u,card c,cardtype ca where u.cardId=c.cardId and c.levelId=ca.level and c.cardId=?", cardId);
        return list;
    }

    @Override
    public int findCardId(int cardID) {
        List<Map> list = JDBC.selectMap("select cardId from user where cardId=?", cardID);
        if (list.size()>0){
            return 1;
        }
        return -1;
    }

    /**
     * 会员登记向card表中添加信息
     * @param card
     */
    @Override
    public void insertCard(Card card) {
        JDBC.sqlUpdate("insert into card(cardId,userId,amount,credit,status,staffId,levelId) values(?,?,?,?,?,?,?)",
                card.getCardId(),
                card.getUserId(),
                card.getAmount(),
                card.getCredit(),
                card.getStatus(),
                card.getStaffId(),
                card.getLevelId()
                );
    }
}
